<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/12/22
 * Time: 下午5:48
 */

class PubBase {

    public static function getPlatform($options=false, $game=false){
        $gameList = PubAuth::getGame();
        $platform = $gameList['Platform'];

        if($game != false && !empty($gameList['Game'][$game]['GameID'])){
            $details  = $gameList['Details'];
            $gameId = $gameList['Game'][$game]['GameID'];
            foreach($platform as  $k=>$v){
                if(!isset($details["{$v['PlatformID']}-{$gameId}"])){
                    unset($platform[$k]);
                }
            }
        }

        if($options){
            $options = array();
            foreach($platform as $v){
                $options[$v['PlatformSrc']] = $v['PlatformName'];
            }
            return $options;
        }else{
            return $platform;
        }
    }

    public static function getGame($options=false, $gameType=false){
        $gameList = PubAuth::getGame();
        $game = $gameList['Game'];

        if($gameType != false){
            foreach($game as $k=>$v){
                if($v['GameType'] != $gameType){
                    unset($game[$k]);
                }
            }
        }

        if($options){
            $options = array();
            foreach($game as $v){
                $options[$v['GameSrc']] = $v['GameName'];
            }
            return $options;
        }else{
            return $game;
        }
    }

    public static function getServer($platform, $game){
        $pubEtl = new PubEtl();

        return $pubEtl->getServer($platform, $game);
    }

    public static function getMenu($game, $platform){
        $tmp = PubAuth::getMenu($game, $platform);

        if(empty($tmp['Menu'])){
            return array();
        }else{
            $menu = $tmp['Menu'];
        }

        foreach($menu as &$v) {
            if(empty($v['MenuUrl'])){
                $v['MenuUrl'] = 'javascript:;';
            }else{
                $v['MenuUrl'] = str_replace(PubAuthConfig::$APP_CUSTOM_MAP[0],$game, $v['MenuUrl']);
                $v['MenuUrl'] = str_replace(PubAuthConfig::$APP_CUSTOM_MAP[1],$platform, $v['MenuUrl']);
                $v['MenuUrl'] = str_replace('&p=','', $v['MenuUrl']);
                $v['MenuUrl'] .= '&m='.$v['MenuID'];
            }
        }
        unset($v);

        $menuTree = array();
        foreach($menu as $v){
            $menuTree[$v['ParentID']][$v['MenuID']] = $v;
        }

        $pubSort = new PubMultiSort();
        $pubSort->setSort('SortNum');
        foreach($menuTree as $k=>&$v){
            $v = $pubSort->getResult($v);
        }
        unset($v);

        return $menuTree;
    }

    public static function getCurrentPlatform(){
        $platform = PubRequest::R('p');

        if(empty($platform)){
            $cookie = Yii::app()->request->getCookies();
            $platform = $cookie['defaultP'];
        }

        if(!isset($platform)){
            $platformList = self::getPlatform(true);
            $platform = key($platformList);
        }

        return $platform;
    }

    public static function getCurrentGame(){
        $game = PubRequest::R('g');

        if(empty($game)){
            $cookie = Yii::app()->request->getCookies();
            $game = $cookie['defaultG'];
        }

        if(!isset($game)){
            $gameList = self::getGame(true);
            $game = key($gameList);
        }

        return $game;
    }

    public static function getAllServer($platform, $game){
        $server = array('ALL');

        $options = PubBase::getServer($platform, $game);
        if(!empty($options)){
            $server = array_merge($server, array_keys($options));
        }

        return $server;
    }

} 